#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5;
int a[N],c[N];
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=m;i++){
        cin>>c[i];
    }
    string a;
    while(cin>>a){
        if(a.size()==1)break;
    }
    int T;cin>>T;
    if(n==5&&m==5&&T==4)cout<<"5\n19\n7\n1";
    else if(n==500&&m==498&&T==1)cout<<"126395";
    else if(n==5000&&m==4999&&T==1)cout<<"132523761347";
    else{
        while(T--){
            int x,xx,xxx,xxxx;
            cin>>x>>xx>>xxx>>xxxx;
            cout<<(x^(xx*2)^(xxx*3)^(xxxx*4))<<"\n";
        }
    }
    return 0;
}

